package com.moody.init;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.moody.hibernate.domain.CodeLib;

/*
 * Copyright © Moody analytics
 *
 * @Author: Yan Jian
 * @Time: 2011-9-9 下午02:45:22
 * @Modified by: 
 * @Description: 地区初始数据处理
 */
public class Region {
	private static final Logger log = LoggerFactory
	.getLogger(DataDictionary.class);
	private static Region instance = null;
	private static Map<String, Map<String, Map<String, String>>> regionMap = new LinkedHashMap<String, Map<String, Map<String, String>>>();
	private static Map<String, Map<String, String>> cityMap = new LinkedHashMap<String, Map<String, String>>();
	
	private Region() {

	}

	public static Region getInstance() {
		if (instance == null) {
			instance = new Region();
		}
		return instance;
	}
	
	public  Map<String, Map<String, Map<String, String>>> getRegion(){
		
		Map<String, CodeLib> region = new LinkedHashMap<String, CodeLib>();
		
		region=DataDictionary.getInstance().getDataDictionary().get("RegionCode");
		
		Iterator iter = region.entrySet().iterator(); 
		while (iter.hasNext()) { 
		    Map.Entry entry = (Map.Entry) iter.next(); 
		    String regionCode = (String) entry.getKey(); 
		    String regionName = ((CodeLib)entry.getValue()).getItemName();
		    if (regionCode.length() == 2){

		    	Map<String, Map<String, String>> cityMap = new LinkedHashMap<String, Map<String, String>>();
		    	if (!regionMap.containsKey(regionName)) {
		    		regionMap.put(regionName,cityMap);
		    	}
		    	continue;
		    }
		    else if (regionCode.length() == 4) {
		    	
		    	String provinces = region.get(regionCode.substring(0, 2)).getItemName();
		    	Map<String, Map<String, String>> cityMap = new LinkedHashMap<String, Map<String, String>>();
		    	Map<String, String> districtMap = new LinkedHashMap<String, String>();
		    	
		    	if (regionMap.containsKey(provinces)) {
		    		regionMap.get(provinces).put(regionName, districtMap);
		    	}

		    	else {
		    		cityMap.put(regionName, districtMap);
		    		regionMap.put(provinces, cityMap);
		    		
		    	}
		    	continue;
		    
		    }

		    else if (regionCode.length() == 6){
		    	String provinceCode = regionCode.substring(0, 2);
		    	String province = region.get(provinceCode).getItemName();
		    	String cities = region.get(regionCode.substring(0, 4)).getItemName();
		    	Map<String, String> districtMap1 = new LinkedHashMap<String, String>();
		    	Map<String, String> districtMap2 = new LinkedHashMap<String, String>();
		    	Map<String, Map<String, String>> cityMap1 = new LinkedHashMap<String, Map<String, String>>();
		    	
		    	if (regionMap.containsKey(province)){
		    		if (provinceCode.equals("11") || provinceCode.equals("12") || provinceCode.equals("31") || provinceCode.equals("50")){
		    			regionMap.get(province).put(regionName, null);
		    		}
		    		else{
			    		if (regionMap.get(province).containsKey(cities)){
			    			//districts.put(regionName, null);
			    			regionMap.get(province).get(cities).put(regionName, null);
			    		}
			    		else {
			    			districtMap1.put(regionName, null);
			    			regionMap.get(province).put(cities, districtMap1);
			    		}
		    		}
		    	}
		    	else {
		    		if (provinceCode.equals("11") || provinceCode.equals("12") || provinceCode.equals("31") || provinceCode.equals("50")){
		    			Map<String, Map<String, String>> cityMap2 = new LinkedHashMap<String, Map<String, String>>();
		    			cityMap2.put(regionName, null);
		    			regionMap.put(province, cityMap2);
		    		}
		    		else {
			    		districtMap2.put(regionName, null);
			    		cityMap1.put(cities, districtMap2);
			    		regionMap.put(province, cityMap1);
		    		}
		    	}
		    	continue;
		    }
		    else {
		    	log.error("Data Format Error!");
		    }   
		}

//		log.info("Region: ", regionMap);

		return regionMap;
	}
	
	public Map<String, Map<String, String>> getCity(){
		
		Map<String, CodeLib> region = new LinkedHashMap<String, CodeLib>();
		region=DataDictionary.getInstance().getDataDictionary().get("RegionCode");
		
		
		Iterator iter = region.entrySet().iterator(); 
		while (iter.hasNext()) { 
		    Map.Entry entry = (Map.Entry) iter.next(); 
		    String regionCode = (String) entry.getKey(); 
		    String regionName = ((CodeLib)entry.getValue()).getItemName();
		    if (regionCode.length() == 2){
		    	if (!cityMap.containsKey(regionCode)) {
		    		cityMap.put(regionCode,new LinkedHashMap<String, String>());
		    	}
		    }
		    else if (regionCode.length() == 4) {
		    	
		    	String provincesCode = regionCode.substring(0, 2);
		    	Map<String, String> districtMap = new LinkedHashMap<String, String>();
		    	
		    	if (cityMap.containsKey(provincesCode)) {
		    		cityMap.get(provincesCode).put(regionCode, regionName);
		    	}

		    	else {
		    		districtMap.put(regionCode, regionName);
		    		cityMap.put(provincesCode, districtMap);
		    		
		    	}
		    
		    }

		    else if (regionCode.length() == 6){
		    	String provinceCode = regionCode.substring(0, 2);
		    	
		    	if (cityMap.containsKey(provinceCode)){
		    		if (provinceCode.equals("11") || provinceCode.equals("12") || provinceCode.equals("31") || provinceCode.equals("50")){
		    			cityMap.get(provinceCode).put(regionCode, regionName);
		    		}
		    	}
		    	else {
		    		if (provinceCode.equals("11") || provinceCode.equals("12") || provinceCode.equals("31") || provinceCode.equals("50")){
		    			Map<String, String> districtMap2 = new LinkedHashMap<String, String>();
		    			districtMap2.put(regionCode, regionName);
		    			cityMap.put(provinceCode, districtMap2);
		    		}
		    	}
		    }
		    else {
		    	log.error("Data Format Error!");
		    }   
		}
		return cityMap;
	}
	
	
	public List getCityJSON(){
		
		Map<String, CodeLib> region = new LinkedHashMap<String, CodeLib>();
		region=DataDictionary.getInstance().getDataDictionary().get("RegionCode");
		
		
		
		List<Map<String,Object>> cityList = new ArrayList<Map<String,Object>>();
		
		Iterator iter = region.entrySet().iterator(); 
		while (iter.hasNext()) { 
		    Map.Entry entry = (Map.Entry) iter.next(); 
		    String regionCode = (String) entry.getKey(); 
		    String regionName = ((CodeLib)entry.getValue()).getItemName();
		    if (regionCode.length() == 2){
		    	Map<String, Object> provinceMap = new LinkedHashMap<String, Object>();
		    	provinceMap.put("provinceCode",regionCode);
		    	provinceMap.put("provinceName", regionName);
		    	provinceMap.put("cities", new ArrayList<Map<String,Object>>());
		    		cityList.add(provinceMap);
		    	
		    }
		    else if (regionCode.length() == 4) {
		    	
		    	String provinceCode = regionCode.substring(0, 2);
		    	
		    	Map<String, String> cityMap = new LinkedHashMap<String, String>();
		    	cityMap.put("cityCode", regionCode);
		    	cityMap.put("cityName", regionName);
		    	for(Map<String,Object> m:cityList){
		    		if(m.get("provinceCode").equals(provinceCode)){
		    			((ArrayList) m.get("cities")).add(cityMap);
		    		}
		    	}
		    
		    }

		    else if (regionCode.length() == 6){
		    	String provinceCode = regionCode.substring(0, 2);
		    	Map<String, String> cityMap = new LinkedHashMap<String, String>();
		    	cityMap.put("cityCode", regionCode);
		    	cityMap.put("cityName", regionName);
		    	
		    		if (provinceCode.equals("11") || provinceCode.equals("12") || provinceCode.equals("31") || provinceCode.equals("50")){
		    			for(Map<String,Object> m:cityList){
				    		if(m.get("provinceCode").equals(provinceCode)){
				    			((ArrayList) m.get("cities")).add(cityMap);
				    		}
				    	}
		    		}
		    	
		    	
		    }
		    else {
		    	log.error("Data Format Error!");
		    }   
		}
		JSONArray jo = JSONArray.fromObject(cityList);
	//	System.out.println(jo);
		return cityList;
	}
	
	public Map getCityMap(){
		
		Map<String, CodeLib> region = new LinkedHashMap<String, CodeLib>();
		region=DataDictionary.getInstance().getDataDictionary().get("RegionCode");

		Map<String,Object> cityMap = new LinkedHashMap<String,Object>();	
		
		Iterator iter = region.entrySet().iterator(); 
		while (iter.hasNext()) { 
		    Map.Entry entry = (Map.Entry) iter.next(); 
		    String regionCode = (String) entry.getKey(); 
		    String regionName = ((CodeLib) entry.getValue()).getItemName();
		    if (regionCode.length() == 4) {
		    	
		    	String provinceCode = regionCode.substring(0, 2);
		    	if(!cityMap.containsKey(provinceCode)){
		    		List<Map<String, String>> city = new ArrayList<Map<String, String>>();
		    		Map<String, String> cityMapT = new LinkedHashMap<String, String>();
			    	cityMapT.put("cityCode", regionCode);
			    	cityMapT.put("cityName", regionName);
			    	city.add(cityMapT);
			    	cityMap.put(provinceCode, city);
		    	}
		    	else {
		    		Map<String, String> cityMapT = new LinkedHashMap<String, String>();
			    	cityMapT.put("cityCode", regionCode);
			    	cityMapT.put("cityName", regionName);
		    		((List)cityMap.get(provinceCode)).add(cityMapT);
		    	}
		    	
		    	
		    
		    }

		    else if (regionCode.length() == 6){
		    	String provinceCode = regionCode.substring(0, 2);
		    	if (provinceCode.equals("11") || provinceCode.equals("12") || provinceCode.equals("31") || provinceCode.equals("50")){
		    	if(!cityMap.containsKey(provinceCode)){
		    		List<Map<String, String>> city = new ArrayList<Map<String, String>>();
		    		Map<String, String> cityMapT = new LinkedHashMap<String, String>();
			    	cityMapT.put("cityCode", regionCode);
			    	cityMapT.put("cityName", regionName);
			    	city.add(cityMapT);
			    	cityMap.put(provinceCode, city);
		    	}
		    	else {
		    		Map<String, String> cityMapT = new LinkedHashMap<String, String>();
			    	cityMapT.put("cityCode", regionCode);
			    	cityMapT.put("cityName", regionName);
		    		((List)cityMap.get(provinceCode)).add(cityMapT);
		    	}
		    	}
		    	
		    		
		    	
		    }
		    else {
		    	//log.error("Data Format Error!");
		    }   
		}
		
		return cityMap;
	}

	public static void main (String args[]){
	System.out.println(	Region.getInstance().getCityMap());
	}

}
